package com.ruoyi.office.asset.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AffectedRows;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PagingData;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanCopier;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.office.asset.domain.AssetStatus;
import com.ruoyi.office.asset.request.AssetStatusCreateDto;
import com.ruoyi.office.asset.request.AssetStatusQueryDto;
import com.ruoyi.office.asset.request.AssetStatusUpdateDto;
import com.ruoyi.office.asset.response.AssetStatusVo;
import com.ruoyi.office.asset.service.IAssetStatusService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 资产状态
 *
 * @author tqfeiyang
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/office/asset/assetStatus")
@RequiredArgsConstructor
public class AssetStatusController extends BaseController
{
    private final IAssetStatusService assetStatusService;

    /**
     * 分页查询资产状态
     */
    @GetMapping("/query")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:query')")
    public R<PagingData<AssetStatusVo>> queryAssetStatusPage(@Validated AssetStatusQueryDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<AssetStatus> list = assetStatusService.selectAssetStatusList(dto);
        return R.ok(PagingData.of(list, AssetStatusVo.class));
    }

    /**
     * 导出资产状态
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:export')")
    public void exportAssetStatusList(HttpServletResponse response, @Validated AssetStatusQueryDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<AssetStatus> list = assetStatusService.selectAssetStatusList(dto);
        new ExcelUtil<>(AssetStatus.class).exportExcel(response, list, "资产状态");
    }

    /**
     * 检索所有资产状态
     */
    @GetMapping("/retrieve")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:retrieve')")
    public R<List<AssetStatus>> retrieveAssetStatusList()
    {
        return R.ok(assetStatusService.selectAssetStatusList(new AssetStatusQueryDto()));
    }

    /**
     * 获取资产状态详细信息
     */
    @GetMapping("/obtain")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:obtain')")
    public R<AssetStatusVo> obtainAssetStatus(@RequestParam Integer id)
    {
        AssetStatus assetStatus = assetStatusService.selectAssetStatusById(id);
        return R.ok(BeanCopier.copy(assetStatus, AssetStatusVo.class));
    }

    /**
     * 新增资产状态
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:create')")
    @Log(title = "资产状态", businessType = BusinessType.INSERT)
    public R<AffectedRows> createAssetStatus(@Validated @RequestBody AssetStatusCreateDto dto)
    {
        AssetStatus assetStatus = BeanCopier.copy(dto, AssetStatus.class);
        assetStatus.setCreateAt(LocalDateTime.now());
        assetStatus.setCreateBy(SecurityUtils.getUsername());
        assetStatus.setUpdateAt(LocalDateTime.now());
        assetStatus.setUpdateBy(SecurityUtils.getUsername());
        return R.rows(assetStatusService.insertAssetStatus(assetStatus));
    }

    /**
     * 修改资产状态
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:update')")
    @Log(title = "资产状态", businessType = BusinessType.UPDATE)
    public R<AffectedRows> updateAssetStatus(@Validated @RequestBody AssetStatusUpdateDto dto)
    {
        AssetStatus assetStatus = BeanCopier.copy(dto, AssetStatus.class);
        assetStatus.setUpdateAt(LocalDateTime.now());
        assetStatus.setUpdateBy(SecurityUtils.getUsername());
        return R.rows(assetStatusService.updateAssetStatus(assetStatus));
    }

    /**
     * 删除资产状态
     */
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:delete')")
    @Log(title = "资产状态", businessType = BusinessType.DELETE)
    public R<AffectedRows> deleteAssetStatusById(@RequestParam Integer id)
    {
        return R.rows(assetStatusService.deleteAssetStatusById(id));
    }

    /**
     * 批量删除资产状态
     */
    @PostMapping("/batchDelete")
    @PreAuthorize("@ss.hasPermi('office:asset:assetStatus:batchDelete')")
    @Log(title = "资产状态", businessType = BusinessType.DELETE)
    public R<AffectedRows> batchDeleteAssetStatusList(@RequestBody Integer[] ids)
    {
        return R.rows(assetStatusService.deleteAssetStatusByIds(ids));
    }
}
